package com.yanqu.road.server.timer.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.timer.GeneralAgentTimerMgr;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SyncActivityRankTask extends GeneralTask {

    private boolean isRun = false;

    private long lastCheckTime;

    @Override
    public void doRun() {
        if(!isRun) {
            isRun = true;
            long nowTime = System.currentTimeMillis();
            List<CrossActivityRankListModel> syncList = new ArrayList<>();
            Map<Integer, ActivityInfo> activityInfoMap = ActivityMgr.getOpenCrossRankActivityMap();

            activityInfoMap.putAll(ActivityMgr.getOpenCrossCommonActivityMap());
            activityInfoMap.putAll(ActivityMgr.getOpenHuaShiGangRankActivityMap());
            if (nowTime - lastCheckTime > GeneralAgentTimerMgr.ALL_SYNC_TIME) {
                for(ActivityInfo activityInfo : activityInfoMap.values()){
                    CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityRankListModel(activityInfo.getActivityId());
                    if(null != rankListModel){
                        syncList.add(rankListModel);
                    }
                }
                lastCheckTime = nowTime;
            } else {
                for(ActivityInfo activityInfo : activityInfoMap.values()){
                    CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityRankListModel(activityInfo.getActivityId());
                    if(null != rankListModel && rankListModel.isNeedSync()){
                        syncList.add(rankListModel);
                    }
                }
            }
            if (syncList.size() > 0) {
                for (CrossActivityRankListModel rankListModel : syncList) {
                    CrossActivityProto.CrossActivityRankListMsg.Builder rankMsg = CrossActivityProto.CrossActivityRankListMsg.newBuilder();
                    rankMsg.setActivityId(rankListModel.getActivityId());
                    rankMsg.setRankList(rankListModel.getRankListMsg());
                    rankListModel.setNeedSync(false);
                    syncToGameServer(rankMsg, activityInfoMap.get(rankListModel.getActivityId()));
                }
            }
            isRun = false;
        }
    }

    private void syncToGameServer(CrossActivityProto.CrossActivityRankListMsg.Builder rankMsg, ActivityInfo activityInfo) {
        if(null == activityInfo || null == rankMsg){
            return;
        }
        for (long serverId : activityInfo.getServerIdList()) {
            MessageHelper.sendPacket(serverId, 0, YanQuMessageUtils.buildMessage(Protocol.S_CROSS_ACTIVITY_SYNC_RANK, rankMsg));
        }
    }
}
